package org.grill.clovercash.storage;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.joda.time.LocalDate;

import com.csvreader.CsvReader;

public class CsvStatement {
	
	private ArrayList<StatementEntry> entries = new ArrayList<StatementEntry>();
	private String filename;
	
	public CsvStatement(String filename) {
		this.filename = filename;
		
		load();
	}
	
	public void load() {
		try {
			CsvReader reader = new CsvReader(filename);
			reader.readHeaders();

			DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
			
			while (reader.readRecord()) {
				StatementEntry entry = new StatementEntry();
				entry.date = new LocalDate(format.parse(reader.get(0)));
				entry.value = new BigDecimal(reader.get(4).replaceAll(",", ""));
				entry.memo = reader.get(3);
				entry.payeeName = reader.get(2);
			
				entries.add(entry);
//				System.out.println(entry);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<StatementEntry> getEntries() {
		return entries;
	}
	
	public static void main(String[] args) {
		String filename = "/Users/dwatson/Desktop/Downloads/Huntington_Delimited.csv";
		
		new CsvStatement(filename);
	}
}
